import 'package:flutter_getx/utils/http.dart';
import 'package:flutter_getx/utils/toast.dart';
import 'package:flutter_getx/utils/page_refresh_mixin.dart';
import 'package:get/get.dart';

import 'state.dart';

class MineLogic extends GetxController with PageRefreshMixin {
  final MineState state = MineState();

  @override
  void onInit() {
    super.onInit();
    // 设置页面自动刷新
    setupPageRefresh(1, getProfile);
  }

  @override
  void onReady() {
    super.onReady();
    // 初始化时加载数据
    getProfile();
  }

  getProfile() async {
    print('我的页面发请求');
    var res = await http.get('/userInfo');
    if (res['code'] != 10000) return ToastUtil.showError('获取数据失败');
    try {
      ToastUtil.showSuccess('我的数据获取');
      state.userInfo = res['data'] as Map<dynamic, dynamic>;
      print(state.userInfo);
    } catch (e) {
      ToastUtil.showError('数据解析失败');
      print(e);
    }
  }
}
